package com.ultrawise.survey.module.user.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ultrawise.survey.core.Page;
import com.ultrawise.survey.core.base.BaseService;
import com.ultrawise.survey.core.base.Saveable;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.user.dao.UsersDao;
import com.ultrawise.survey.module.user.vo.TSurveyInfoUsers;

@Service("usersService")
public class UsersService extends BaseService {
	@Autowired
	private UsersDao usersDao;
	@Autowired
	private BaseService baseService;

	// 根据条件查询表中数据
	public Page searchUsers(Map<String, String> map, Saveable objForSave,
			final Class obj, final int pageNo, final int pageSize)
			throws ServiceException {
		Page<TSurveyInfoUsers> page = null;
		try {
			page = usersDao
					.getUsersInfo(map, objForSave, obj, pageNo, pageSize);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);
		}
		return page;
	}

	// 根据条件查询表中数据
	public Page searchObj(Map<String, String> map, Saveable objForSave,
			final Class obj, final int pageNo, final int pageSize)
			throws ServiceException {
		Page<TSurveyInfoUsers> page = null;
		try {
			page = usersDao.getObjectList(map, objForSave, obj, pageNo,
					pageSize);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);
		}
		return page;
	}

	/******
	 * 只能通过用户的唯一标志位：id对象进行查询，并且返回其用户对象
	 * @param user
	 * @return
	 */
	public TSurveyInfoUsers getUser(TSurveyInfoUsers user) {
		if (user != null && user.getId() != null) {
			TSurveyInfoUsers dto = (TSurveyInfoUsers) usersDao.findByID(
					user.getId(), user, TSurveyInfoUsers.class);
			return dto;
		}
		return null;
	}

	/****
	 * 更新用户的最后登录时间跟登录次数
	 * 
	 * @param user
	 * @throws ServiceException
	 */
	// public void simpleUpdate(TSurveyInfoUsers user) throws ServiceException {
	// TSurveyInfoUsers userinfo = getUser(user);
	// if (userinfo != null && userinfo.getId() != null) {
	// int lgcount = (userinfo.getLogincount() != null) ? userinfo
	// .getLogincount() : 0;
	// lgcount += 1;
	// userinfo.setLogincount(lgcount);
	// userinfo.setLasttime(DateUtils.toStringAll(new Date()));
	// userinfo.setLoginip(user.getLoginip());// 设置ip
	// try {
	// baseService.modifyObj(user);
	// } catch (ServiceException e) {
	// throw new ServiceException("更新用户登录信息异常", e);
	// }
	// }
	//
	// }

	/****
	 * 修改密码，保存密码信息
	 * 
	 * @param user
	 * @throws ServiceException
	 */
	public void savePassword(TSurveyInfoUsers user) throws ServiceException {
		TSurveyInfoUsers userinfo = getUser(user);
		if (userinfo != null && userinfo.getId() != null) {
			userinfo.setUs_passwd(user.getUs_passwd());
			baseService.updateObj(userinfo);
		}
	}

	/*****
	 * 新用户调用方法 当用户对象信息不能查询匹配信息就进行新加操作，否则返回空信息。
	 * 
	 * @param user
	 * @return
	 */
	public TSurveyInfoUsers addUsers(TSurveyInfoUsers user)
			throws ServiceException {
		if (!isExistUsers(user)) {
			if (user != null && user.getUs_login() != null) {
				int max = (Integer) this.getMaxID(user);
				user.setId(max);
			}
			return usersDao.addUsers(user);
		}
		return null;
	}

	/*****
	 * 翻页获取用户表的page对象信息
	 * 
	 * @param dto
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @throws ServiceException
	 */
	public Page<TSurveyInfoUsers> findUsersPages(TSurveyInfoUsers dto,
			int pageNo, int pageSize) throws ServiceException {
		Page<TSurveyInfoUsers> page = null;
		try {
			page = usersDao.getUsersPages(dto, pageNo, pageSize);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);
		}
		return page;
	}

	/*****
	 * 判断用户是否存在
	 * 
	 * @param user
	 * @return
	 */
	public boolean isExistUsers(TSurveyInfoUsers user) {
		boolean isexist = false;
		List us = usersDao.findByColunm("US_LOGIN", user.getUs_login(), user,
				TSurveyInfoUsers.class);
		if (us != null && us.size() > 0) {
			isexist = true;
		}
		return isexist;
	}

	/****
	 * 返回全部用户信息
	 * 
	 * @return
	 */
	public List<TSurveyInfoUsers> getUsersAll() {
		return usersDao.findAll(new TSurveyInfoUsers(), TSurveyInfoUsers.class);
	}

	/**
	 * 判断用户是否存在
	 */
	public boolean isExistUser(String strLogin) {

		return usersDao.isExist(strLogin);

	}

	/**
	 * 删除一个用户
	 */
	public boolean delUser(String id) {

		return usersDao.delUser(id);

	}

	/**
	 * 查询用户
	 */

	public Object getUser(String colunm, String value, Saveable objForSave,
			Class obj) throws ServiceException {
		try {
			return (Object) usersDao.findUser(colunm, value, objForSave, obj);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);

		}
	}

	/**
	 * 对用户进行模糊查询
	 */
	public Object FindUser(String colunm, String value, Saveable objForSave,
			Class obj) throws ServiceException {
		try {
			return (Object) usersDao.GetUser(colunm, value, objForSave, obj);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);

		}
	}

	/**
	 * 查询全部用户
	 */
	public Object FindAllUser(String colunm, String value, Saveable objForSave,
			Class obj) throws ServiceException {
		try {
			return (Object) usersDao
					.findAllUser(colunm, value, objForSave, obj);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);

		}
	}

}
